package com.simple.datax.bo.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.datax.bo.IDataxReceiveChannelBO;
import com.simple.datax.dao.IDataxReceiveChannelDAO;
import com.simple.datax.vo.DataxReceiveChannelVO;
import com.simple.util.common.DateUtil;
import com.simple.util.object.PageInfoBean;

@Service("dataxReceiveChannelBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class DataxReceiveChannelBOImpl implements IDataxReceiveChannelBO {

	@Autowired
	private IDataxReceiveChannelDAO dataxReceiveChannelDAO;
	
	@Override
	public DataxReceiveChannelVO findById(Integer fiId) {
		return dataxReceiveChannelDAO.findById(fiId);
	}

	@Override
	public List<DataxReceiveChannelVO> findAll() {
		return dataxReceiveChannelDAO.findAll();
	}

	@Override
	public List<DataxReceiveChannelVO> findDataxReceiveChannelVOByPage(
			PageInfoBean pageInfoBean, String fiId, String fcCommunType,
			String fcMsgFormatId, String fcMsgType) {
		return dataxReceiveChannelDAO.findDataxReceiveChannelVOByPage(pageInfoBean, fiId, fcCommunType, fcMsgFormatId, fcMsgType);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void add(DataxReceiveChannelVO dataxReceiveChannelVO) {
		dataxReceiveChannelVO.setFiGroup(1);
		dataxReceiveChannelVO.setFdTime(DateUtil.getNowDate());
		dataxReceiveChannelDAO.saveOrUpdate(dataxReceiveChannelVO);		
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void edit(DataxReceiveChannelVO dataxReceiveChannelVO) {
		final DataxReceiveChannelVO drcv = dataxReceiveChannelDAO.findById(dataxReceiveChannelVO.getFiId());

		drcv.setFcChannelDesc(dataxReceiveChannelVO.getFcChannelDesc());
		drcv.setFcCommunParam(dataxReceiveChannelVO.getFcCommunParam());
		drcv.setFcCommunType(dataxReceiveChannelVO.getFcCommunType());
		drcv.setFcMsgCharset(dataxReceiveChannelVO.getFcMsgCharset());
		drcv.setFcMsgFormateId(dataxReceiveChannelVO.getFcMsgFormateId());
		drcv.setFcMsgType(dataxReceiveChannelVO.getFcMsgType());
		drcv.setFiFlag(dataxReceiveChannelVO.getFiFlag());
		
		drcv.setFiGroup(1);
		drcv.setFdTime(DateUtil.getNowDate());
		dataxReceiveChannelDAO.saveOrUpdate(drcv);
	}

	@Override
	public Integer generateId() {
		return dataxReceiveChannelDAO.generateId();
	}

	@Override
	public Map<String, String> findSysReceiveIdMap() {
		 List<DataxReceiveChannelVO> dataxReceiveChannelVOList = dataxReceiveChannelDAO.findAll();
		 final Map<String ,String> map = new LinkedHashMap<String ,String>();
		 for(final DataxReceiveChannelVO dataxReceiveChannelVO: dataxReceiveChannelVOList){
			 map.put(String.valueOf(dataxReceiveChannelVO.getFiId()), dataxReceiveChannelVO.getFcChannelDesc());
		 }
		 return map;
	}

	@Override
	public List<DataxReceiveChannelVO> findReceiveChannelVOByPage(
			PageInfoBean pageInfoBean, String fcChannelDesc) {
		return dataxReceiveChannelDAO.findReceiveChannelVOByPage(pageInfoBean, fcChannelDesc);
	}

}
